/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jeemicro.weixin.modules.timer_quartz.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jeemicro.weixin.common.config.Global;
import com.jeemicro.weixin.common.persistence.Page;
import com.jeemicro.weixin.common.web.BaseController;
import com.jeemicro.weixin.common.utils.StringUtils;
import com.jeemicro.weixin.modules.timer_quartz.entity.TimerQuartz;
import com.jeemicro.weixin.modules.timer_quartz.service.TimerQuartzService;
import com.jeemicro.weixin.modules.timer_quartz.utils.KettleJob;
import com.jeemicro.weixin.modules.timer_quartz.utils.QuartzManager;

/**
 * 定时器管理配置Controller
 * @author 宋雅健
 * @version 2017-05-15
 */
@Controller
@RequestMapping(value = "${adminPath}/timer_quartz/timerQuartz")
public class TimerQuartzController extends BaseController {

	@Autowired
	private TimerQuartzService timerQuartzService;
	
	@ModelAttribute
	public TimerQuartz get(@RequestParam(required=false) String id) {
		TimerQuartz entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = timerQuartzService.get(id);
		}
		if (entity == null){
			entity = new TimerQuartz();
		}
		return entity;
	}
	
	@RequiresPermissions("timer_quartz:timerQuartz:view")	
	@RequestMapping(value = {"list", ""})
	public String list(TimerQuartz timerQuartz, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TimerQuartz> page = timerQuartzService.findPage(new Page<TimerQuartz>(request, response), timerQuartz); 
		model.addAttribute("page", page);
		return "modules/timer_quartz/timerQuartzList";
	}

	@RequiresPermissions("timer_quartz:timerQuartz:view")
	@RequestMapping(value = "form")
	public String form(TimerQuartz timerQuartz, Model model) {
		model.addAttribute("timerQuartz", timerQuartz);
		return "modules/timer_quartz/timerQuartzForm";
	}

	@RequiresPermissions("timer_quartz:timerQuartz:edit")
	@RequestMapping(value = "save")
	public String save(TimerQuartz timerQuartz, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, timerQuartz)){
			return form(timerQuartz, model);
		}
		timerQuartzService.save(timerQuartz);
		addMessage(redirectAttributes, "保存定时器成功");
		return "redirect:"+Global.getAdminPath()+"/timer_quartz/timerQuartz/?repage";
	}
	
	@RequiresPermissions("timer_quartz:timerQuartz:edit")
	@RequestMapping(value = "delete")
	public String delete(TimerQuartz timerQuartz, RedirectAttributes redirectAttributes) {
		timerQuartzService.delete(timerQuartz);
		addMessage(redirectAttributes, "删除定时器成功");
		return "redirect:"+Global.getAdminPath()+"/timer_quartz/timerQuartz/?repage";
	}
	
	@RequestMapping(value = "startup")
	public String startup(TimerQuartz timerQuartz, RedirectAttributes redirectAttributes) {
		 String job_name = "动态任务调度";  
         System.out.println("【系统启动】开始(每1秒输出一次)...");    
         QuartzManager.addJob(job_name, KettleJob.class, timerQuartz.getTriggercron());    
		return "redirect:"+Global.getAdminPath()+"/timer_quartz/timerQuartz/?repage";
	}
	
	@RequestMapping(value = "stop")
	public String stop(TimerQuartz timerQuartz, RedirectAttributes redirectAttributes) {
		 String job_name = "动态任务调度";  
         System.out.println("【系统启动】开始(每1秒输出一次)...");    
         QuartzManager.removeJob(job_name);  
//		return "";
		return "redirect:"+Global.getAdminPath()+"/timer_quartz/timerQuartz/?repage";
	}

}